package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.Server;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class ArmorSkillSound {
  public static final String TOKEN = ",";
  
  private static final ArrayList<ArrayList<Object>> aData15c = new ArrayList<>();
  
  private static boolean NO_MORE_GET_DATA15c = false;
  
  private static ArmorSkillSound _instance;
  
  private ArmorSkillSound() {
    getData15c();
  }
  
  public static ArmorSkillSound getInstance() {
    if (_instance == null)
      _instance = new ArmorSkillSound(); 
    return _instance;
  }
  
  public static void reload() {
    aData15c.clear();
    _instance = null;
    getInstance();
  }
  
  public static void main(String[] a) {
    try {
      Server.main(null);
    } catch (Exception exception) {}
  }
  
  public static void forArmorSkillSound(L1PcInstance pc) {
    ArrayList<Object> aTempData = null;
    if (!NO_MORE_GET_DATA15c) {
      NO_MORE_GET_DATA15c = true;
      getData15c();
    } 
    int i = 0;
    while (i < aData15c.size()) {
      aTempData = aData15c.get(i);
      if ((int[])aTempData.get(0) != null && 
        pc.getInventory().checkEquipped((int[])aTempData.get(0))) {
        pc.sendPackets((ServerBasePacket)new S_SkillSound(pc.getId(), ((Integer)aTempData.get(1)).intValue()));
        pc.broadcastPacketAll((ServerBasePacket)new S_SkillSound(pc.getId(), ((Integer)aTempData.get(1)).intValue()));
      } 
      i++;
    } 
  }
  
  private static void getData15c() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_装备持续特效");
      ArrayList<Object> aReturn = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList<Object>();
          if (rset.getString("armor_id") != null && !rset.getString("armor_id").equals("") && 
            !rset.getString("armor_id").equals("0")) {
            aReturn.add(0, convert(rset.getString("armor_id").split(",")));
          } else {
            aReturn.add(0, null);
          } 
          aReturn.add(1, new Integer(rset.getInt("gfxId")));
          aData15c.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static int[] convert(String[] args) {
    int[] i32 = new int[args.length];
    int j = 0;
    while (j < args.length) {
      i32[j] = Integer.parseInt(args[j]);
      j++;
    } 
    return i32;
  }
}
